package com.java.zhuiyun.api.sys_user.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.zhuiyun.api.sys_menu.service.IMenuService;
import com.java.zhuiyun.api.sys_user.entity.Password;
import com.java.zhuiyun.api.sys_user.entity.SysUser;
import com.java.zhuiyun.api.sys_user.mapper.SysUserMapper;
import com.java.zhuiyun.api.sys_user.service.ISysUserService;
import com.java.zhuiyun.common.response.CommonResult;

import com.java.zhuiyun.util.pageUtil.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName SysUserController
 * @Description TODO
 * @Author xy
 * @Date 2023/8/14 20:01
 **/
@RestController
@RequestMapping("/User")
public class SysUserController {

    @Autowired
    ISysUserService sysUserService;

    @Resource
    SysUserMapper sysUserMapper;

    @Resource
    IMenuService menuService;

    /**
    * @Author xy
    * @Description 全查
    * @Date 2023/8/15 18:50
    * @Return  * @param null
    */
    @GetMapping("/SysUserList")
    public CommonResult userList(SysUser sysUser, PageRequest pageRequest){
        return CommonResult.ok(sysUserService.userList(sysUser,pageRequest));
    }

    /**
    * @Author xy
    * @Description 添加
    * @Date 2023/8/15 20:14
    * @Return  * @param null
    */
    @PostMapping("/SysUserAdd")
    public CommonResult SysDictAdd( SysUser sysUser, MultipartFile file){
        int i = sysUserService.SysUserAdd(sysUser,file);
        if (i>0){
            return CommonResult.ok("恭喜您，添加成功");
        }else {
            return CommonResult.error("很抱歉，添加失败");
        }
    }

    /**
     * @Author xy
     * @Description 修改
     * @Date 2023/8/15 16:26
     * @Return  * @param null
     */
    @PostMapping("/SysUserUpdate")
    public CommonResult SysUserUpdate(SysUser sysUser, MultipartFile file){
        int i = sysUserService.SysUserUpdate(sysUser,file);
        if (i>0){
            return CommonResult.ok("恭喜您，修改成功");
        }else {
            return CommonResult.error("很抱歉，修改失败");
        }
    }

    @PostMapping("/updatescope")
    public CommonResult updatescope(@RequestBody SysUser sysUser) {
        sysUserService.updatescope(sysUser);
        return CommonResult.ok("禁用成功");
    }

    /**
     * @Author xy
     * @Description 逻辑删除
     * @Date 2023/8/15 16:41
     * @Return  * @param null
     */
    @DeleteMapping("/SysUserDelete")
    public CommonResult SysUserDelete(Integer id){
        int i = sysUserService.SysUserDelete(id);
        if (i>0){
            return CommonResult.ok("恭喜您，删除成功");
        }else {
            return CommonResult.error("很抱歉，删除失败");
        }
    }

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public CommonResult success() {
        return CommonResult.ok();
    }

    /**
    * @Author xy
    * @Description 登录
    * @Date 2023/8/17 15:12
    * @Return  * @param null
    */
    @PostMapping("/login")
    public CommonResult login(@RequestBody SysUser sysUser){
        CommonResult commonResult = sysUserService.login(sysUser);
        return commonResult;
    }

//    /**
//     * 注册
//     * @param sysUser
//     * @return
//     */
//    @PostMapping("/register")
//    public CommonResult register(@RequestBody SysUser sysUser) {
//        String userName = sysUser.getUserName();
//        String password = sysUser.getPassword();
//        // 检查用户名和密码是否为空
//        if (StrUtil.isBlank(userName) || StrUtil.isBlank(password)) {
//            return CommonResult.error("参数错误");
//        }
//        Example example = new Example(SysUser.class);
//        example.createCriteria().andEqualTo("userName",userName);
//        SysUser dbUser = sysUserMapper.selectOneByExample(example);
//        if (dbUser != null) {
//            // 如果用户名已存在，则返回错误信息
//            return CommonResult.error("用户名已存在");
//        }
//        // 如果用户昵称为空，则设置一个默认昵称
//        if (StrUtil.isBlank(sysUser.getNickName())) {
//            sysUser.setNickName("小可爱" + RandomUtil.randomNumbers(6) + "号");
//        }
//        // 将用户信息插入数据库
//        int insert = sysUserMapper.insert(sysUser);
//        System.out.println(insert);
//        return CommonResult.ok();
//    }
//
//    /**
//     * 修改密码
//     * @param sysUser
//     * @return
//     */
//    @PutMapping("/password")
//    public CommonResult password(@RequestBody SysUser sysUser) {
//        if (StrUtil.isBlank(sysUser.getUserName()) || StrUtil.isBlank(sysUser.getPassword()) || StrUtil.isBlank(sysUser.getNewPass())) {
//            // 检查参数是否为空
//            return CommonResult.error("参数错误");
//        }
//        // 根据用户名和原密码更新数据库中的密码
//        sysUserMapper.updateByExample(null, new UpdateWrapper<SysUser>().set("password", sysUser.getNewPass())
//                .eq("userName", sysUser.getUserName()).eq("password", sysUser.getPassword()));
//        return CommonResult.ok();
//    }
//
    @PostMapping("/updateInfo")
    public CommonResult saveInfo(@RequestBody SysUser sysUser){
        return sysUserService.saveInfo(sysUser);
    }
//    /**
//     * 重置密码
//     * @param sysUser
//     * @return
//     */
//    @PutMapping("/resetPassword")
//    public CommonResult resetPassword(@RequestBody SysUser sysUser) {
//        // 检查用户名和手机号是否为空
//        if (StrUtil.isBlank(sysUser.getUserName()) || StrUtil.isBlank(sysUser.getPhone())) {
//            return CommonResult.error("参数错误");
//        }
//        // 构建查询条件，通过用户名和手机号查询用户信息
//        Example example = new Example(SysUser.class);
//        example.createCriteria().andEqualTo("userName", sysUser.getUserName());
//        SysUser existingUser = sysUserMapper.selectOneByExample(example);
//        if (existingUser != null) {
//            // 如果找到匹配的用户信息，则重置密码为默认密码"123"
//            existingUser.setPassword("123");
//            sysUserMapper.updateByPrimaryKey(existingUser);
//            return CommonResult.ok("密码重置成功");
//        } else {
//            // 如果未找到匹配的用户信息，则返回错误信息
//            return CommonResult.error("未找到用户");
//    }
//    }


    /**
     * 根据用户名获取数据库表的所有用户属性
     * @param userName
     * @return
     */
    @GetMapping("/userName/{userName}")
    public CommonResult findByUsername(@PathVariable String userName){
        SysUser sysUserBack1 = sysUserMapper.selectList(userName);
        return CommonResult.ok(sysUserBack1);
    }

    //根据角色查询
    @GetMapping("/role/{role}")
    public CommonResult findUsersByRole(@PathVariable String role){
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role",role);
        List<SysUser> list = sysUserMapper.selectByExample(queryWrapper);
        return CommonResult.ok(list);
    }

    /**
     * @Author xy
     * @Description 获取当前用户的菜单列表
     * @Date 2023/8/22 10:16
     * @Return  * @param null
     */
    @GetMapping("/MenuList")
    public CommonResult MenuList(){
        return CommonResult.ok(menuService.MenuList());
    }

    @GetMapping("/selectById")
    public CommonResult selectById(Integer id){
        return CommonResult.ok(sysUserMapper.selectById(id));
    }

    @PostMapping("/savePassword")
    public CommonResult savePassword(@RequestBody Password password){
        return sysUserService.savePassword(password);
    }
}
